package com.taxi.management.controller;

import com.taxi.management.entity.DriverDetailEntity;
import com.taxi.management.entity.ResultEntity;
import com.taxi.management.entity.UserEntity;
import com.taxi.management.service.DriverDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RequestMapping("v3/driverDetail/")
@CrossOrigin//解决跨域问题
@RestController//Json
public class DriverDetailController {
    @Autowired
    private DriverDetailService driverDetailService;

    // 插入司机详情
    @PostMapping("insert")
    public ResultEntity insert(@RequestBody DriverDetailEntity entity) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(driverDetailService.insert(entity));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据添加成功！");
        } catch (RuntimeException e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    // 根据ID删除司机详情
    @DeleteMapping("delete/{driverId}")
    public ResultEntity deleteById(@PathVariable Integer driverId) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(driverDetailService.deleteById(driverId));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    // 更新司机详情
    @PutMapping("update")
    public ResultEntity update(@RequestBody DriverDetailEntity entity) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(driverDetailService.update(entity));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据更新成功！");

        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 根据ID查询司机详情
    @GetMapping("select/{driverId}")
    public ResultEntity selectById(@PathVariable Integer driverId) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(driverDetailService.selectById(driverId));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 查询所有司机详情
    @GetMapping("selectAll")
    public ResultEntity selectAll() {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(driverDetailService.selectAll());
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
            resultEntity.setCount(driverDetailService.selectAll().size());
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 分页查询司机详情
    @GetMapping("selectByPage")
    public ResultEntity selectByPage(@RequestParam  String username, @RequestParam int page, @RequestParam int limit) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            int start=(page-1)*limit;
            int cnt=-1;
            List<DriverDetailEntity> list = driverDetailService.selectByPage(username,start,limit);
            if(!username.equals("")){
                cnt=list.size();//有查询条件的时候，就按查询回来的条数计
            }else{
                cnt= driverDetailService.selectAll().size();//当没有查询条件的时候 就获取所有用户数量
            }
            resultEntity.setData(list);
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
            resultEntity.setCount(cnt);
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 查询总数
    @GetMapping("getTotalCount")
    public ResultEntity getTotalCount() {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCount(driverDetailService.getTotalCount());
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
            e.printStackTrace();
        }
        return resultEntity;
    }
}
